//
//  EPMComposeView.m
//  WeiBoByOC
//
//  Created by 马继鵬 on 17/4/1.
//  Copyright © 2017年 7TH. All rights reserved.
//

#import "EPMComposeView.h"
#import "EPMComposeModel.h"
#import "EPMComposeBtn.h"

@interface EPMComposeView()
@property (strong,nonatomic) UIViewController* target;
@property (strong,nonatomic) NSMutableArray<EPMComposeBtn*>* btnList;
@end

@implementation EPMComposeView

- (NSMutableArray<EPMComposeBtn*>*)btnList{
    if (_btnList==nil){
        _btnList = [NSMutableArray<EPMComposeBtn*> new];
    }
    return _btnList;
}

- (void)showComposeViewwithTarget:(UIViewController *)target
{
    self.target = target;
    [target.view addSubview:self];
    
    [self popToScreeen:YES];
}



- (instancetype)initWithFrame:(CGRect)frame{
    CGRect selfFrame = [UIScreen mainScreen].bounds;
    if (self = [super initWithFrame:selfFrame]){
        [self setupUI];
    }
    return self;
}


- (void)setupUI{
    self.backgroundColor = [UIColor colorWithPatternImage:[[UIImage getCurrentScreenSnap]applyLightEffect]];
    UIImageView* imgAD = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"compose_slogan"]];
    [self addSubview:imgAD];
    [imgAD sizeToFit];
    [imgAD mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(100);
    }];
    
    [self loadBtnList];
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self popToScreeen:NO];
    dispatch_queue_t queue = dispatch_get_main_queue();
    
    dispatch_async(queue, ^{
         [self removeFromSuperview];
    });
    
    
    
   
}

#pragma
#pragma-添加btn数组

- (void)loadBtnList{
    //获得btn的信息
    NSString* file = [[NSBundle mainBundle] pathForResource:@"compose.plist" ofType:nil];
    NSArray* tempArray = [NSArray arrayWithContentsOfFile:file];
    NSArray* modelList = [NSArray yy_modelArrayWithClass:[EPMComposeModel class] json:tempArray];
    
    //设置btn的间距
    CGFloat btnWidth = 80;
    CGFloat btnHeight = 120;
    CGFloat btnMargine = (screenWidth - 3*btnWidth)/4;
    
    for (int i = 0 ; i < 6 ; i ++){
        CGFloat intX = btnMargine + (btnMargine + btnWidth)*(i%3);
        CGFloat intY = (btnHeight+btnMargine)* (i/3);
        EPMComposeModel* model = modelList[i];
        EPMComposeBtn* btn = [[EPMComposeBtn alloc]initWithFrame:CGRectMake(intX, intY+screenHeight, btnWidth, btnHeight)];
        [self.btnList addObject:btn];
        btn.model = model;
       
        [btn setImage:[UIImage imageNamed:model.icon] forState:UIControlStateNormal];
        [btn setTitle:model.title forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(composeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];

    }
}

- (void) popToScreeen:(BOOL)isUpwarding{
 
    CGFloat moveY = isUpwarding ? -350 : 350;
    NSMutableArray<EPMComposeBtn*>* deverseArr = [[self.btnList reverseObjectEnumerator] allObjects].mutableCopy;
    
    
    NSMutableArray<EPMComposeBtn*>* temp = isUpwarding ? self.btnList : deverseArr;
    [temp enumerateObjectsUsingBlock:^(EPMComposeBtn * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
          POPSpringAnimation* anSpring = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
          anSpring.toValue = [NSValue valueWithCGPoint:CGPointMake(obj.center.x, obj.center.y+moveY)];
          anSpring.springBounciness = 10.0f;
          anSpring.beginTime = CACurrentMediaTime() + self.btnList.count * 0.025;
         
          [obj pop_addAnimation:anSpring forKey:nil];
      }];
    
    
}
#pragma
#pragma-点击事件
    - (void)composeBtnClicked:(EPMComposeBtn*) btn{
        [UIView animateWithDuration:0.25 animations:^{
            for (EPMComposeBtn* button in self.btnList){
                button.alpha = 0.2;
                if (button == btn){
                    btn.transform = CGAffineTransformMakeScale(2, 2);
                }else{
                    button.transform = CGAffineTransformMakeScale(0.5, 0.5);
                }
            }
            
            
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.25 delay:0 options:0 animations:^{
                for (EPMComposeBtn* button in self.btnList){
                    button.alpha = 1;
                    button.transform = CGAffineTransformIdentity;
                }
            } completion:^(BOOL finished) {
                //判断是否是第一个跳转到下一个控制器
                if ([btn.model.classname isEqualToString:@"EPMComposeViewController"]){
                    
                    UIViewController* controller = [NSClassFromString(btn.model.classname) new];
                    UINavigationController* navController = [[UINavigationController alloc]initWithRootViewController:controller];
                    
                    
                    [self.target presentViewController:navController animated:YES completion:^{
                        [self removeFromSuperview];
                    }];
                    
                }
                
                
                
            }];
        }];
        
        
    }
    
    






@end
